package com.kzd.gtboon.main.game

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import com.kzd.gtboon.R
import com.kzd.gtboon.custom.setTransitionStatusE
import com.kzd.gtboon.databinding.FragmentGameClassifyBinding
import com.kzd.gtboon.jetpack.vm.GameViewModel
import com.kzd.gtboon.main.game.adapter.GameClassifyAdapter
import com.module.lemlin.http.Status
import com.module.lemlin.owner.OwnerViewBindingFragment
import com.module.lemlin.owner.ownerViewModel

class GameClassifyFragment : OwnerViewBindingFragment<FragmentGameClassifyBinding>() {

    companion object {
        fun newInstance(): GameClassifyFragment {
            return GameClassifyFragment()
        }
    }

    private val mGameClassifyAdapter = GameClassifyAdapter()

    private val mViewModel:GameViewModel by lazy {
        ownerViewModel(owner = this)
    }

    override val inflate: (LayoutInflater, ViewGroup?, Boolean) -> FragmentGameClassifyBinding
        get() = FragmentGameClassifyBinding::inflate

    override fun initView() {
        mViewBinding.includeAppRefresh.rvAppRefreshBody.apply {
            layoutManager = LinearLayoutManager(requireContext())
        }.adapter = mGameClassifyAdapter

        mGameClassifyAdapter.setOnItemClickListener { _, _, position ->
            if (mGameClassifyAdapter.selectedPosition == position)
                return@setOnItemClickListener
            val itemData = mGameClassifyAdapter.data[position]
            mViewModel.selectedClassifyIdLiveData.postValue(
                GameViewModel.GameUpdate(itemData.id)
            )
        }

        mViewBinding.includeAppRefresh.mSmartRefreshLayout.setOnRefreshListener {
            mViewModel.gameClassify()
        }
    }

    override fun initData() {
        mViewModel.gameClassify()
    }

    override fun initViewMode() {
        mViewModel.gameClassifyLiveData.observe(viewLifecycleOwner, { response ->
            setTransitionStatusE(response,mGameClassifyAdapter,mViewBinding.includeAppRefresh.mSmartRefreshLayout,(false))
            if (response.status != Status.SUCCESS)
                return@observe
            val data = response.data?.data
            if (data.isNullOrEmpty())
                return@observe
            mViewBinding.mlMotionLayout.transitionToEnd()
            mViewBinding.includeAppRefresh.mSmartRefreshLayout.setEnableRefresh(false)
            mGameClassifyAdapter.setNewInstance(data)
            mViewModel.selectedClassifyIdLiveData.postValue(
                GameViewModel.GameUpdate(data[0].id)
            )
        })

        mViewModel.selectedClassifyIdLiveData.observe(viewLifecycleOwner, { update ->
            val index = mGameClassifyAdapter.data
                .indexOfLast { it.id == update.type }
            if (index == -1 || index == 0) {
                showFeaturedFragment1()
            } else {
                showListFragment1(update.type)
            }
            mGameClassifyAdapter.selectedPosition = index
        })

    }


    private fun showFeaturedFragment1() {
        childFragmentManager.beginTransaction().apply {
            replace(R.id.flGameClassifyBody, GameFeaturedFragment.newInstance())
        }.commit()
    }

    private fun showListFragment1(type:String) {
        childFragmentManager.beginTransaction().apply {
            replace(R.id.flGameClassifyBody, GameListFragment.newInstance(type))
        }.commit()
    }
}